<template>
  <div>
    <div class="topNav">
      <van-nav-bar
        :title="this.aid ? '编辑地址' : '新增地址'"
        left-text="返回"
        left-arrow
        @click-left="quit"
      />
    </div>
    <div class="addressadd">
    <van-address-edit
    :area-list="areaList"
      show-postal
      :show-delete="!!this.aid"
      show-set-default
      show-search-result
      :address-info="address"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="save"
      @delete="deleteAddress"
    />
    </div>
  </div>
</template>

<script>
import { areaList } from '@vant/area-data';
export default {
  name: "NewAddress",
  data() {
    return {
        areaList,
        aid:"",
        address:{}
    };
  },
  methods: {
    quit() {
      this.$router.go(-1);
    },
    // 保存地址
    save(content){
        if(this.aid){
          this.edit(content)
        }else{
          this.add(content)
        }
    },
    // 编辑地址
    edit(content){
      let addressData = {...content}

        addressData.isDefault = Number(addressData.isDefault)
        delete addressData.country

        let token = this.$cookies.get('TOKEN')

      this.axios({
        method: "POST",
        url:'/editAddress',
        data: {
          tokenString: token,
          ...addressData,
          aid:this.aid
        }
      })
        .then((res) => {
          
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)
            return
          }
          this.$toast(res.data.msg)
          
          if(res.data.code === 30000){
              setTimeout(() => {
              // this.$router.push({name: 'Address'});
              this.$router.go(-1)
            }, 2000)
          }
        })
        .catch((err) => {
          
        });
    },
    // 新增地址
    add(content){
      let addressData = {...content}

        addressData.isDefault = Number(addressData.isDefault)
        delete addressData.country

        let token = this.$cookies.get('TOKEN')

      this.axios({
        method: "POST",
        url:'/addAddress',
        data: {
          tokenString: token,
          ...addressData
        }
      })
        .then((res) => {
          
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)
            return
          }
          this.$toast(res.data.msg)
          
          if(res.data.code === 9000){
              setTimeout(() => {
              // this.$router.push({name: 'Address'});
              this.$router.go(-1)
            }, 2000)
          }
        })
        .catch((err) => {
          
        });
    },
    // 若有aid，则初始化地址
    getAddress(){
      let token = this.$cookies.get('TOKEN')

      this.axios({
        method: "GET",
        url:'/findAddressByAid',
        params: {
          tokenString: token,
          aid:this.aid
        }
      })
        .then((res) => {
          
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)
            return
          }
          
          if (res.data.code === 40000) {
            let data =  res.data.result[0];
            data.isDefault = Boolean(data.isDefault);

            this.address = data;
          }
        })
        .catch((err) => {
          
        });
    },
    // 删除地址
    deleteAddress(){
      let token = this.$cookies.get('TOKEN')

      this.axios({
        method: "POST",
        url:'/deleteAddress',
        data: {
          tokenString: token,
          aid:this.aid
        }
      })
        .then((res) => {
          
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)
            return
          }
          
         if (res.data.code === 10000) {
            setTimeout(() => {
              this.$router.push({name: 'Address'});
            }, 2000)
          }
        })
        .catch((err) => {
          
        });
    }
  },
  created(){
    this.aid = this.$route.params.aid
    if(this.aid){
      this.getAddress()
    }
  }
};
</script>

<style lang="less" scoped>
.topNav {
  position: sticky;
  top: 0;
  /deep/ .van-nav-bar__text{
      color: #0c34ba;
  }
  /deep/ .van-nav-bar .van-icon{
      color: #0c34ba;
  }
}
.addressadd{
   /deep/ .van-button--danger{
       background-color: #0c34ba;
       border: 1px solid #0c34ba;
   }
   /deep/ .van-switch--on{
       background-color: #0c34ba;
   }
}
</style>